//
//  WorkorderLoginViewController.m
//  CarLife
//
//  Created by yu on 2018/1/24.
//  Copyright © 2018年 jpas. All rights reserved.
//

#import "WorkorderLoginViewController.h"
#import "WorkorderViewController.h"
#import "UIView+TransitionAnimation.h"
#import "NSString+JudgeString.h"
#import "LocationManager.h"

@interface WorkorderLoginViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *logoImgView;
@property (weak, nonatomic) IBOutlet UILabel *accountLabel;
@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UILabel *lineView;
@property (weak, nonatomic) IBOutlet UIButton *rememberBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation WorkorderLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.fd_prefersNavigationBarHidden = YES;
    
    [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 120.f;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self addAllConstraints];
    
    if (self.user.isSaveWorkorderAccount) {
        self.rememberBtn.selected = YES;
        self.accountTextField.text = self.user.workorderAccount;
    }
}

- (IBAction)loginAction:(UIButton *)sender
{
    if (![LocationManager isLocationEnabled]) {// 用户拒绝开启定位权限
        [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请在系统设置中开启定位服务(设置>隐私>定位服务>开启)，上传您的位置" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去设置", nil] show];
        return;
    }
    
    if (![NSString valiMobile:self.accountTextField.text]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"请输入正确的帐号" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
        return;
    }
    
    [self.accountTextField resignFirstResponder];
    
    [self.requestTool workorderLogin:self.accountTextField.text isSaveAccount:self.user.isSaveWorkorderAccount success:^(int status, NSString *installId) {
        if (status == 0) {
            WorkorderViewController *wo = [[WorkorderViewController alloc] init];
            wo.installId = installId;
            [self.navigationController pushViewController:wo animated:YES];
        } else {
            [[[UIAlertView alloc] initWithTitle:@"登录失败，请重新登录" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil] show];
        }
    } failure:^(NSError *error) {
        PPLog(@"workerorder login error = %@",error);
    }];
}
- (IBAction)rememberAction:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [self.user setSaveWorkorderAccountStatus:@(1)];
    } else {
        [self.user setSaveWorkorderAccountStatus:@(0)];
    }
}

- (void)setLoginBtn:(UIButton *)loginBtn {
    _loginBtn = loginBtn;
    if (@available(iOS 13.0, *)) {
        UIColor *loginColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                return kBaseColor;
            } else {
                return kBaseColorDark20;
            }
        }];
        [_loginBtn setBackgroundColor:loginColor];
    } else {
        [_loginBtn setBackgroundColor:kBaseColor];
    }
}

#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
    }
}

- (void)addAllConstraints
{
    _scrollView = [[UIScrollView alloc] init];
    //    _scrollView.alwaysBounceVertical = YES;
    //    _scrollView.backgroundColor = [UIColor baseCLBlueColor];
    [self.view addSubview:_scrollView];
    
    _scrollView.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    
    [_scrollView sd_addSubviews:@[_logoImgView,_accountLabel,_accountTextField,_rememberBtn,_lineView,_loginBtn]];
    
    CGFloat margin10 = 10; CGFloat margin15 = 15;
    CGFloat margin20 = 20; CGFloat margin30 = 30;
    
    _logoImgView.sd_layout
    .leftEqualToView(_scrollView)
    .rightEqualToView(_scrollView)
    .topEqualToView(_scrollView)
    .autoHeightRatio(0.63);
//
    _accountLabel.sd_layout
    .leftSpaceToView(_scrollView, margin30)
    .topSpaceToView(_logoImgView, margin20*2)
    .rightSpaceToView(_scrollView, margin30)
    .heightIs(margin30);
//
    _accountTextField.sd_layout
    .leftEqualToView(_accountLabel)
    .rightEqualToView(_accountLabel)
    .topSpaceToView(_accountLabel, margin30)
    .heightIs(35);
//
    _lineView.sd_layout
    .leftEqualToView(_accountTextField)
    .rightEqualToView(_accountTextField)
    .topSpaceToView(_accountTextField, margin10)
    .heightIs(1);
    
    _rememberBtn.sd_layout
    .centerYEqualToView(_accountTextField)
    .rightEqualToView(_accountTextField)
    .widthIs(25)
    .heightEqualToWidth();
    
    _loginBtn.sd_layout
    .leftEqualToView(_lineView)
    .rightEqualToView(_lineView)
    .topSpaceToView(_lineView, kAutoHeight(margin20*4))
    .heightIs(45);
    
    
    [_scrollView setupAutoContentSizeWithBottomView:_loginBtn bottomMargin:margin20*2];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
